package com.czzapp.xiaoyumemo.model;

import java.io.File;

import com.czzapp.xiaoyumemo.provider.MemoProvider;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class AudioManager {
	private static AudioManager sIntance = null;
	private static final String[] AUDIO_REF_PROJECTIONS = new String[] {
		MemoProvider.AUDIO_TABLE_ID,
		MemoProvider.PATH, 
		MemoProvider.AUDIO_REFERENCE
	};
	private static final String WHERE_PATH = new String(MemoProvider.PATH + " = ?");
	private static final int AUDIO_ID_INDEX = 0;
	private static final int PATH_INDEX = 1;
	private static final int REF_INDEX = 2;
	
	private Context mContext = null;
	
	public static void init(Context context) {
		sIntance = new AudioManager(context);
	}
	
	public static AudioManager getInstance() {
		return sIntance;
	}
	
	private AudioManager(Context context) {
		mContext = context;
	}
	
	public void addAudioRef(String audioPath) {
		Cursor cursor = mContext.getContentResolver().query(MemoProvider.AUDIO_CONTENT_URI,
				AUDIO_REF_PROJECTIONS,
				WHERE_PATH,
				new String[] { audioPath },
				null);
		boolean isFound = false;
		if(cursor != null) {
			if(cursor.moveToFirst()) {
				isFound = true;
				int refCount = cursor.getInt(REF_INDEX);
				refCount++;
				int audioId = cursor.getInt(AUDIO_ID_INDEX);
				ContentValues audioContentValues = new ContentValues();
				audioContentValues.put(MemoProvider.AUDIO_REFERENCE, refCount);
				Uri audioUri = ContentUris.withAppendedId(MemoProvider.AUDIO_CONTENT_URI, audioId);
				mContext.getContentResolver().update(audioUri, audioContentValues, null, null);
			}
		cursor.close();
		}
		if(!isFound) {
			ContentValues audioContentValues = new ContentValues();
			audioContentValues.put(MemoProvider.PATH, audioPath);
			audioContentValues.put(MemoProvider.AUDIO_REFERENCE, 1);
			mContext.getContentResolver().insert(MemoProvider.AUDIO_CONTENT_URI, audioContentValues);
		}
	}
	
	public void delAudioRef(String audioPath) {
		Cursor cursor = mContext.getContentResolver().query(MemoProvider.AUDIO_CONTENT_URI,
				AUDIO_REF_PROJECTIONS,
				WHERE_PATH,
				new String[] { audioPath },
				null);
		if(cursor != null) {
			if(cursor.moveToFirst()) {
				int audioId = 0;
				int refCount = 0;
				audioId = cursor.getInt(AUDIO_ID_INDEX);
				refCount = cursor.getInt(REF_INDEX);
				if(refCount == 1) {
					Uri audioUri = ContentUris.withAppendedId(MemoProvider.AUDIO_CONTENT_URI, audioId);
					mContext.getContentResolver().delete(audioUri, null, null);
					File audioFile = new File(audioPath);
					if(audioFile.exists()) {
						audioFile.delete();
					}
				}
				else {
					refCount--;
					ContentValues audioContentValues = new ContentValues();
					audioContentValues.put(MemoProvider.AUDIO_REFERENCE, refCount);
					Uri memoUri = ContentUris.withAppendedId(MemoProvider.AUDIO_CONTENT_URI, audioId);
					mContext.getContentResolver().update(memoUri, audioContentValues, null, null);
				}
			}
		cursor.close();
		}
	}
}